package file;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.result.DeleteResult;
import mongo.MongoTest;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.bson.Document;
import util.DateUtil;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * USER: lintc 【lintiancong@zhuojianchina.com】
 * DATE: 2017-07-18 15:25
 */
public class FileToMongo implements Runnable{

    private static Logger LOG = Logger.getLogger(FileToMongo.class);

    private String fileName;

    private String timeStr;

    private String topic;

    public FileToMongo(String fileName, String timeStr, String topic) {
        this.fileName = fileName;
        this.timeStr = timeStr;
        this.topic = topic;
    }

    @Override
    public void run() {
        long stTime = System.currentTimeMillis();
        MongoCollection<Document> collection = MongoTest.getCollection("api_log_" + timeStr);
        Document d = new Document("topic", topic);
        DeleteResult result = collection.deleteMany(d);
        System.out.println("删除： " + result.getDeletedCount());
        int i = 0;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new FileInputStream(fileName), "UTF-8"));
            JSONObject obj;
            List<JSONObject> list = new ArrayList<>();
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                i++;
                obj = getJson(line);
                if (null != obj) {
                    list.add(obj);
                }
                if (list.size() == 50) { // 每50条执行一次插入
                    long start = System.currentTimeMillis();
                    MongoTest.insertList(collection, list);
                    long end = System.currentTimeMillis();
                    LOG.info(topic + " : " + timeStr + " ： 添加成功 ：" + i + " : " + (end - start));
                    list = new ArrayList<>();
                }
            }
            if (list.size() > 0) {
                long start = System.currentTimeMillis();
                MongoTest.insertList(collection, list);
                long end = System.currentTimeMillis();
                LOG.info(topic + " : " + timeStr + " ： 添加成功 ：" + i + " : " + (end - start));
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(timeStr + " : " + (System.currentTimeMillis() - stTime) + " : " + i + " : 结束");
    }

    public JSONObject getJson(String value) {
        String times;
        int i = value.indexOf("{");
        String timeStr = value.substring(0, 23);
        times = value.substring(0, 10);
        if (!DateUtil.isTime(times)) {
            return null;
        }
        value = value.substring(i, value.length());
        JSONObject obj = JSONObject.fromObject(value);
        obj.put("time", timeStr);
        obj.put("topic", topic);
        return obj;
    }

    public static void main(String[] args) throws InterruptedException {
        String[] tList = {"2017-06-18"};
        for (String timeStr : tList) {
            FileToMongo hzpt = new FileToMongo("F:\\filet\\default.log." + timeStr, timeStr, "app_hzpt");
            new Thread(hzpt).start();
            Thread.sleep(8 * 1000);
        }
    }
}
